﻿/*========================== 
 * @author 郑金泉
 * @desc 数据库配置信息类
 * ========================= */
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Obsidian.Utils;

namespace Obsidian.Config
{
    public class DbConfig
    {
        private static DateTime modified = DateTime.MinValue;
        private static Hashtable htDb = Hashtable.Synchronized(new Hashtable());

        public static DbInfo GetDbInfo(string alias)
        {
            CheckForChanged();
            if (!htDb.ContainsKey(alias))
                return null;
            return (DbInfo)htDb[alias];
        }

        private static void CheckForChanged()
        {
            if (AppConfig.CheckForChanged() || !AppConfig.Modified.Equals(modified))
            {
                GetDbInfos();
                modified = AppConfig.Modified;
            }
        }

        private static void GetDbInfos()
        {
            lock (htDb)
            {
                htDb.Clear();
                XmlDocument xmlDoc = AppConfig.GetXml();
                XmlNodeList nodeList = xmlDoc.SelectNodes("appConfig/dbs/db");
                for (int i = 0; i < nodeList.Count; i++)
                {
                    DbInfo dbInfo = new DbInfo();
                    XmlNode node = nodeList.Item(i);
                    string val;
                    if (!XmlUtil.TryGetAttrValue(node, "alias", out val))
                        throw new Exception("alias属性不能为空!");
                    dbInfo.SetAlias(val);

                    if (!XmlUtil.TryGetAttrValue(node, "connectionString", out val))
                        throw new Exception("connectionString属性不能为空!");
                    dbInfo.SetConnectionString(val);

                    if (!XmlUtil.TryGetAttrValue(node, "type", out val))
                        throw new Exception("type属性不能为空!");
                    dbInfo.SetType(val);

                    if (XmlUtil.TryGetAttrValue(node, "owner", out val))
                        dbInfo.SetOwner(val);

                    if (htDb.ContainsKey(dbInfo.Alias))
                        throw new Exception("存在重复名称：" + dbInfo.Alias);

                    htDb.Add(dbInfo.Alias, dbInfo);
                }
            }
        }

        public class DbInfo
        {
            private string _alias;
            private string _connectionString;
            private string _type;
            private string _owner;

            public string Alias 
            {
                get { return _alias; } 
            }

            internal void SetAlias(string val)
            {
                this._alias = val;
            }

            public string ConnectionString
            {
                get { return _connectionString; }
            }

            internal void SetConnectionString(string val)
            {
                this._connectionString = val;
            }

            public string Type 
            {
                get { return _type; }
            }

            public string Owner
            {
                get { return this._owner; }
            }

            internal void SetType(string val)
            {
                this._type = val;
            }

            internal void SetOwner(string owner)
            {
                this._owner = owner;
            }

        }
    }
}
